package o;

import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.IllegalFormatException;
import java.util.Locale;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public abstract class eux {
    private static String ˊ = "AESUtil";

    private static Cipher ˎ(int i, byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        Key secretKeySpec = new SecretKeySpec(bArr, "AES");
        Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
        instance.init(i, secretKeySpec, new IvParameterSpec(bArr2));
        return instance;
    }

    public static String ॱ(String str, String str2, byte[] bArr) throws UnsupportedEncodingException, GeneralSecurityException {
        if (str2 == null || str == null) {
            Logger.e(ˊ, "encrypt error, sKey or sSrc is null");
            return null;
        } else if (str2.length() >= 16) {
            if (str2.length() > 16) {
                if (Logger.ˎ()) {
                    Logger.ˏ(ˊ, "key len is more than 16, cut out");
                }
                str2 = str2.substring(0, 16);
            }
            return ˎ(ˎ(1, str2.getBytes("ASCII"), bArr).doFinal(str.getBytes("UTF-8")));
        } else if (!Logger.ˎ()) {
            return null;
        } else {
            Logger.ˏ(ˊ, "key len is less than 16");
            return null;
        }
    }

    private static String ˎ(byte[] bArr) {
        StringBuilder stringBuilder = new StringBuilder(64);
        for (byte b : bArr) {
            String toHexString = Integer.toHexString(b & 255);
            if (toHexString.length() == 1) {
                stringBuilder.append("0");
                stringBuilder.append(toHexString);
            } else {
                stringBuilder.append(toHexString);
            }
        }
        return stringBuilder.toString().toUpperCase(Locale.US);
    }

    public static String ॱ(String str, byte[] bArr, byte[] bArr2) throws GeneralSecurityException, UnsupportedEncodingException {
        if (bArr == null || bArr.length < 16) {
            return null;
        }
        if (bArr.length > 16) {
            bArr = dnj.ˏ(bArr, 16);
        }
        return dnk.ˏ(ˎ(1, bArr, bArr2).doFinal(str.getBytes("UTF-8"))).replaceAll("\n", "").replaceAll("\r", "");
    }

    public static String ˊ(String str, byte[] bArr) throws UnsupportedEncodingException {
        Object ʽ = epr.ˎ().ʽ();
        if (!TextUtils.isEmpty(ʽ)) {
            return ˏ(str, ʽ.getBytes("UTF-8"), bArr);
        }
        Logger.e("AESUtil", "secretKey is null");
        return null;
    }

    public static String ˏ(String str, byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr.length < 16) {
            return null;
        }
        try {
            if (bArr.length > 16) {
                bArr = dnj.ˏ(bArr, 16);
            }
            return new String(ˎ(2, bArr, bArr2).doFinal(dnk.ˎ(str)), "UTF-8");
        } catch (Exception e) {
            Logger.ˋ(ˊ, "AESBaseDecrypt error");
            return null;
        }
    }

    public static String ॱ(String str) {
        String str2 = null;
        if (dof.ˋ(str)) {
            return str2;
        }
        try {
            return ॱ(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            Logger.e(ˊ, "can not getBytes");
            return str2;
        }
    }

    public static String ॱ(@NonNull byte[] bArr) {
        try {
            MessageDigest instance = MessageDigest.getInstance(AaidIdConstant.SIGNATURE_SHA256);
            instance.update(bArr);
            StringBuilder stringBuilder = new StringBuilder(256);
            for (byte b : instance.digest()) {
                stringBuilder.append(String.format(Locale.ENGLISH, "%02X", new Object[]{Byte.valueOf(b)}));
            }
            return stringBuilder.toString();
        } catch (NoSuchAlgorithmException e) {
            Logger.e(ˊ, "sha256EncryptStr error:NoSuchAlgorithmException");
            return null;
        } catch (IllegalFormatException e2) {
            Logger.e(ˊ, "sha256EncryptStr error:IllegalFormatException");
            return null;
        } catch (Exception e3) {
            Logger.e(ˊ, "sha256EncryptStr error:Exception");
            return null;
        }
    }

    public static String ˎ(String str) {
        if (!TextUtils.isEmpty(str)) {
            try {
                String[] split = new String(dnk.ˎ(str), "UTF-8").split("\\|");
                if (split.length > 1) {
                    str = evh.ˊ().ॱ(split[0], split[1]);
                }
            } catch (Exception e) {
                Logger.e(ˊ, "getDecryptString error");
            }
        }
        return str;
    }

    public static String ˊ(String str) {
        if (!TextUtils.isEmpty(str)) {
            try {
                String ˏ = dnk.ˏ(evh.ˋ());
                String ˎ = evh.ˊ().ˎ(str, ˏ);
                StringBuilder stringBuilder = new StringBuilder(DnsConfig.MAX_CACHE_ENTRIES);
                stringBuilder.append(ˎ);
                stringBuilder.append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR);
                stringBuilder.append(ˏ);
                str = dnk.ˏ(stringBuilder.toString().getBytes("UTF-8"));
            } catch (Exception e) {
                Logger.e(ˊ, "getEncryptString error");
            }
        }
        return str;
    }
}
